#include <pthread.h>
#include <stdio.h>
#include "StdThread.h"
#include <stdlib.h>
struct StdThread
{
    pthread_t threadID;

};

Thread *InitThread(void *(*func)(void *), void *arg)
{
    Thread* t = (Thread*)malloc(sizeof(Thread));
    if(t == NULL)
    {
        printf("InitThread malloc error!\n");
        return NULL;
    }
    if(pthread_create(&t->threadID,NULL,func,arg) == 0 )
    {
        return t;
    }
    else
    {
        printf("create pthread error!\n");
        return NULL;
    }


    return NULL;
}

unsigned long int GetThreadID(Thread *t)
{
    return t->threadID;
}

void CloseThread(Thread *t)
{
    if(pthread_cancel(t->threadID) == 0 )
    {
        free(t);
    }
    else
    {
        printf("close thread error!\n");
    }
}

void* JoinThread(Thread *t)
{
    void* value;
    pthread_join(t->threadID,&value);
    return value ;
}

void DetachThread(Thread *t)
{
    if(pthread_detach(t->threadID)!=0)
    {
        printf("DetachThread error!\n");
    }
}
